<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.sn" placeholder="质量考核通知单"></el-input>
                </el-form-item>
                <el-form-item label="录入时间">
                    <el-date-picker
                            v-model="filters.betweenTime"
                            type="daterange"
                            value-format="yyyy-MM-dd"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" v-on:click="getdept">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <!--		:data :绑定的数据-->
        <!--		v-loading: 页面css样式等东西加载之后加载加载数据-->
        <!--		prop:对应的是:data绑定的对象的属性-->
        <!--		label :描述-->
        <el-table :data="dept" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="选择" width="60">
            </el-table-column>
            <el-table-column prop="sn" label="质量考核通知单" width="190" sortable>
            </el-table-column>
            <el-table-column prop="email" label="重要度等级" width="120" sortable>

            </el-table-column>
            <el-table-column prop="phone" label="供货商编码" width="120" sortable>
            </el-table-column>

            <!--			<el-table-column prop="state" label="状态" width="100" :formatter="formatSex" sortable>-->
            <!--				<template slot-scope="scope">-->
            <!--					<el-button type="text" size="small" @click="suppstop(scope.$index, scope.row)"-->
            <!--							   :type="scope.row.state === 1?'primary':'danger'"-->
            <!--					>-->
            <!--						{{scope.row.state===1?"启用":"禁用"}}-->
            <!--					</el-button>-->
            <!--				</template>-->
            <!--			</el-table-column>-->

            <el-table-column prop="username" label="供货商名称" width="120" sortable>
            </el-table-column>
            <el-table-column prop="password" label="零件号" width="100" sortable>
            </el-table-column>
            <el-table-column prop="name" label="零件名称" width="120" sortable>
            </el-table-column>
            <el-table-column prop="logininfo" label="批次号" width="100" sortable>
            </el-table-column>
            <el-table-column prop="onsaletime" label="纳入起始时间" width="180" sortable>
            </el-table-column>
            <el-table-column prop="offsaletime" label="纳入截止时间" width="180" sortable>
            </el-table-column>

            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
<!--            <el-table-column label="审核" width="150">-->
<!--                <template slot-scope="scope">-->
<!--                    <el-button type="success" size="small" @click="handlestate(scope.$index, scope.row)">操作状态-->
<!--                    </el-button>-->

<!--                </template>-->
<!--            </el-table-column>-->
        </el-table>

        <!--分页条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <!--			current-change:自带的事件,我当前页发生改变时,就会触发此事件-->
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="5"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <el-dialog title="操作状态" :visible.sync="Visible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item>
                    <el-switch
                            style="display: block"
                            v-model="state"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-text="通过"
                            inactive-text="驳回">
                    </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="Visible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubm" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--编辑界面-->
        <el-dialog title="操作" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="质量考核通知单" prop="sn">
                    <el-input v-model="editForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="重要等级" prop="email">
                    <el-input v-model="editForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="供应商编码" prop="phone">
                    <el-input v-model="editForm.phone" auto-complete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="供应商名称" prop="username">
                    <el-input v-model="editForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="零件号" prop="password">
                    <el-input v-model="editForm.password" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="零件名称" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="批次号" prop="logininfo">
                    <el-input v-model="editForm.logininfo" auto-complete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="纳入起始时间" prop="onsaletime">
                    <el-input v-model="editForm.onsaletime" auto-complete="off" disabled></el-input>
                </el-form-item>
<!--                <el-form-item label="纳入截止时间" prop="offsaletime">-->
<!--                    <el-input v-model="editForm.offsaletime" auto-complete="off"></el-input>-->
<!--                </el-form-item>-->
                <el-form-item label="纳入截止日期">
                <el-date-picker
                        v-model="editForm.offsaletime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>



            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>


    </section>
</template>

<script>

    //import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    //import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    sn:'',
                    keyword:'',
                    betweenTime:'',
                    username: '',
                    parentId: null,
                    managerId: null,
                },

                parentDepts: [],
                manageremps: [],
                managerS: [],
                treeList: [],
                dept: [],
                total: 0,
                page: 1,
                size: 5,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                Visible: false,
                editLoading: false,
                editFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },
                state: true,
                //编辑界面数据
                editForm: {
                    id: '',
                    name: '',
                    tel: '',
                    state: '',
                    address: '',
                    adminId: '',
                    logininfo:'',
                    onsaletime:'',
                    offsaletime:'',
                },
            }
        },
        methods: {
            //性别显示转换
            //row 当前行row = user  column当前列
            formatSex: function (row, column) {
                return row.state == 1 ? '正常' : row.state == 0 ? '禁用' : '未知';
            },
            handleCurrentChange(val) {
                //var 就是当前页的数据
                this.page = val;
                this.getdept();
            },
            //获取部门列表
            getdept(flag) {
                //封装的数据
                let para = {
                    //page 就是当前页
                    page: this.page,
                    size: this.size,
                    //这个是高级查询的条件
                    keyword: this.filters.sn,
                };
                this.listLoading = true;
                //NProgress.start();
                this.$http.post("/zhi/findPage", para).then((res) => {
                    // console.log("后端发送过来的分页数据:"+JSON.stringify(res));
                    //
                    console.log(res)
                    this.total = res.data.total;
                    //
                    this.dept = res.data.list;
                    //跳转页面
                    if (this.total % 5 == 0 && flag != 2) {
                        this.page = this.page - 1
                        this.getdept(2)
                    }
                    //关闭进度条
                    this.listLoading = false;

                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {id: row.id};
                    this.$http.delete("/zhi/" + row.id).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getdept();
                    });
                }).catch(() => {

                });
            },
            handlestate: function (index, row) {
                this.Visible = true;
                this.editForm = row;
                console.log("当前修改的数据是:" + JSON.stringify(this.editForm))
                this.state = !!row.state
            },

            //显示编辑界面
            handleEdit: function (index, row) {
                //显示编辑页面的模态框
                this.editFormVisible = true;
                //Object.assign 是拷贝对象 BeanUtil.copyof()
                this.editForm = row;
                //显示正常状态
                this.state = !!row.state
                let dirPath = row.dirPath;
                console.log(dirPath)
                let paths = dirPath.split("/");
                paths = paths.slice(1, paths.length - 1);
                paths = paths.map(Number)
                this.editForm.parentIds = paths
                console.log("当前修改的数据是:" + JSON.stringify(this.editForm))
            },
            //新增显示
            handleAdd: function () {
                //显示编辑页面的模态框
                this.editFormVisible = true;
                //清空模态框
                this.state = true
                this.editForm = {
                    name: '',
                    sn: '',
                }

            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //这里面是获取数据的
                            this.editLoading = true;
                            //状态
                            this.editForm.state = this.state ? 1 : 0
                            //拷贝对象
                            let para = Object.assign({}, this.editForm);
                            this.$http.post("/zhi", para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: res.data.success ? '提交成功' : res.data.message,
                                    type: res.data.success ? 'success' : 'error'
                                    // message: '提交成功',
                                    // type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                //关闭模态框
                                this.editFormVisible = false;
                                this.getdept();
                            });
                        });
                    }
                });
            },

            editSubm: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //这里面是获取数据的
                            this.editLoading = true;
                            //状态
                            this.editForm.state = this.state ? 1 : 0
                            //拷贝对象
                            let para = Object.assign({}, this.editForm);
                            this.$http.post("/zhi", para).then((res) => {
                                this.editLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: res.data.success ? '提交成功' : res.data.message,
                                    type: res.data.success ? 'success' : 'error'
                                    // message: '提交成功',
                                    // type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                //关闭模态框
                                this.Visible = false;
                                this.getdept();
                            });
                        });
                    }
                });
            },

            //复选勾选框的方法
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        'ids': ids,

                    };
                    this.$http.post("/zhi/deleteIds/" + ids).then((res) => {
                        console.log(res)
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getdept();
                    });
                }).catch(() => {

                });
            },
            parentDept() {
                this.$http.post("/dept/parentDept").then(res => {
                    console.log("1")
                    console.log(res)
                    this.parentDepts = res.data.data
                })
            },
            managers() {
                this.$http.get("/emp/managerAll").then(res => {
                    console.log("2")
                    console.log(res)
                    this.manageremps = res.data.data
                })
            },
            managerSI() {
                this.$http.get("/emp").then(res => {
                    console.log(res)
                    this.managerS = res.data.data
                })
            },
            treeLists() {
                this.$http.get("/dept/treeList").then(res => {
                    console.log(res)
                    this.treeList = res.data.data
                })
            },

        },
        //就是页面已加载完成就会去触发这个mounted
        mounted() {

            this.getdept();
            //查询上级部门
            this.parentDept();
            //查询经理们
            this.managers();
            //新增经理们
            this.managerSI();
            //级联选择器
            this.treeLists();

        }
    }

</script>

<style scoped>

</style>
